package com.wlgc.service.impl;

import com.wlgc.entity.Assigment.Assignment;
import com.wlgc.entity.Assigment.Student_Assigment;
import com.wlgc.entity.RestBean;
import com.wlgc.mapper.AssignmentMapper;
import com.wlgc.service.AssignmentService;
import com.wlgc.service.CourseService;
import com.wlgc.service.StudentService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AssignmentServiceImpl implements AssignmentService {

    @Resource
    AssignmentMapper assignmentMapper;
    @Resource
    StudentService studentService;
    @Resource
    CourseService courseService;


    public RestBean getClass_Course_Condition(int refer, int cno, int cid) {
        if (refer == 0) {
            return new RestBean<>(200, "", assignmentMapper.getClassCourseUF(cno, cid));
        } else if (refer == 1) {
            return new RestBean<>(200, "", assignmentMapper.getClassCourseF(cno, cid));
        } else if (refer == 10) {
            List<Student_Assigment> list = assignmentMapper.getClassCourseF(cno, cid);
            assignmentMapper.getClassCourseUF(cno, cid).forEach(student_assigment -> {
                list.add(student_assigment);
            });

            return new RestBean<>(200, "", list);
        } else
            return new RestBean<>(500, "请求失败，完成状态错误！");
    }

    @Override
    public RestBean get_Course_Condition(int refer, int cid) {
        List<Student_Assigment> list;
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        int sid = studentService.getStudentSidByUsername(username);

        if (refer == 0) {
            list = assignmentMapper.getCourseById(sid, cid, "未完成");
        } else if (refer == 1) {
            list = assignmentMapper.getCourseById(sid, cid, "完成");

        } else {
            list = assignmentMapper.getAllCourseById(sid, cid);


        }
        return new RestBean<>(200, "", list);
    }

    @Override
    public RestBean get_Course_Condition(int refer, int cid, int sid) {
        List<Student_Assigment> list;
        if (refer == 0) {
            list = assignmentMapper.getCourseById(sid, cid, "未完成");
        } else if (refer == 1) {
            list = assignmentMapper.getCourseById(sid, cid, "完成");

        } else {
            list = assignmentMapper.getAllCourseById(sid, cid);
        }
        return new RestBean<>(200, "", list);
    }

    //将中文转化为参数
    @Override
    public String translate(String refer) {
        if (refer.equals("全部")) {
            refer = "10";
        } else if (refer.equals("已完成")) {
            refer = "1";
        } else if (refer.equals("未完成")) {
            refer = "0";
        }
        return refer;
    }

    @Override
    public List<Student_Assigment> getCourseAllAssignmentByCourseName(String courseName, int cno) {
        List<Student_Assigment> assignments = assignmentMapper.getCourseAllAssignmentByCourseName(courseName, cno);
        if (!assignments.isEmpty())
            return assignments;
        return null;
    }

    @Transactional
    @Override
    public boolean addAssignment(String courseName, String cno, String assignmentName, String detail) {
        int flag = 1;
        //如果cno 为全部班级
        if (cno.equals("全部班级")) {
            flag = 10;
        }
        //先插入作业
        try {
            assignmentMapper.addAssignment(assignmentName, detail);
            int cid = Integer.parseInt(courseService.getCid(courseName));
            int aid = assignmentMapper.getAidByAssignmentName(assignmentName);
            assignmentMapper.addCourse_Assignment(cid, aid);
            if (flag == 1) {
                assignmentMapper.addStudent_Assignment(aid, Integer.parseInt(cno));
            } else {
                //所有选择了该课程的同学都添加作业
                assignmentMapper.addStudent_Assignment_All(aid);
            }
        } catch (Exception exception) {
            return false;
        }
        return true;
    }
}
